import util from '@/assets/js/util';
import axios from './axios'

export default {
  // 上传文件
  fileUpload(file,icon,data,token,cb) {
    var formData = new FormData()
    formData.append('file', file)
    formData.append('file', icon)
    formData.append('client_app', JSON.stringify(data))
    formData.append('oper_token', token)
    // console.log(formData.forEach)
  //console.log(formData.get('file'))
  //console.log(formData.get('client_app'))
  //console.log(formData.get('oper_token'))
    return axios.http('/AndroidPlatService/Files/FileUpload', 'post', formData, cb);
  },


  /**
   * operator
   */
  // 登录
  login(data) {
    data.loading = true
    return axios.http('/Oper/Login', 'post', data);
  },
  // 查询操作员列表
  getOperators(data) {
    data.loading = true
    return axios.http('/Oper/GetRec', 'get', data);
  },
  // 创建操作员
  createOper(data) {
    data.loading = true
		return axios.http('/Oper/Create', 'post', data)
	},
  // 修改操作员
  updateOper(data) {
    data.loading = true
		return axios.http('/Oper/Update', 'post', data)
	},
  // 批量设置操作员
  batchOper(data) {
    data.loading = true
		return axios.http('/Oper/Batch', 'post', data)
	},
  // 删除操作员
  delOper(data) {
    data.loading = true
		return axios.http('/Oper/Delete', 'post', data)
	},

  /**
   * client
   */
  // 查询用户列表
  getClients(data) {
    data.loading = true
    return axios.http('/Client/GetRec', 'get', data);
  },
  // 创建用户
  createClient(data) {
    data.loading = true
		return axios.http('/Client/Create', 'post', data)
	},
  // 修改用户
  updateClient(data) {
    data.loading = true
		return axios.http('/Client/Update', 'post', data)
	},
  // 批量设置用户
  batchClient(data) {
    data.loading = true
		return axios.http('/Client/Batch', 'post', data)
	},
  // 删除用户
  delClient(data) {
    data.loading = true
		return axios.http('/Client/Delete', 'post', data)
	},

  /**
   * rom
   */
  // 查询固件列表
  getRoms(data) {
    data.loading = true
    return axios.http('/Rom/GetRec', 'get', data);
  },
  // 创建固件
  createRom(data) {
    data.loading = true
		return axios.http('/Rom/Create', 'post', data)
	},
  // 修改固件
  updateRom(data) {
    data.loading = true
		return axios.http('/Rom/Update', 'post', data)
	},
  // 批量设置固件
  batchRom(data) {
    data.loading = true
		return axios.http('/Rom/Batch', 'post', data)
	},
  // 删除固件
  delRom(data) {
    data.loading = true
		return axios.http('/Rom/Delete', 'post', data)
	},

  /**
   * model
   */
  // 查询设备型号
  getModel(data) {
    data.loading = true
    return axios.http('/Model/GetRec', 'get', data);
  },
  // 创建设备型号
  createModel(data) {
    data.loading = true
		return axios.http('/Model/Create', 'post', data)
	},
  // 修改设备型号
  updateModel(data) {
    data.loading = true
		return axios.http('/Model/Update', 'post', data)
	},
  // 批量设置设备型号
  batchModel(data) {
    data.loading = true
		return axios.http('/Model/Batch', 'post', data)
	},
  // 删除设备型号
  delModel(data) {
    data.loading = true
		return axios.http('/Model/Delete', 'post', data)
	},

  /**
   * device
   */
  // 查询设备
  getDevice(data) {
    data.loading = true
    return axios.http('/Device/GetRec', 'get', data);
  },
  // 创建设备
  createDevice(data) {
    data.loading = true
		return axios.http('/Device/Create', 'post', data)
	},
  // 修改设备
  updateDevice(data) {
    data.loading = true
		return axios.http('/Device/Update', 'post', data)
	},
  // 批量设置设备
  batchDevice(data) {
    data.loading = true
		return axios.http('/Device/Batch', 'post', data)
	},
  // 删除设备
  delDevice(data) {
    data.loading = true
		return axios.http('/Device/Delete', 'post', data)
	},

  /**
   * app
   */
  // 查询应用
  getApps(data) {
    data.loading = true
    return axios.http('/App/GetRec', 'get', data);
  },
  // 创建应用
  createApp(data) {
    data.loading = true
		return axios.http('/App/Create', 'post', data)
	},
  // 修改应用
  updateApp(data) {
    data.loading = true
		return axios.http('/App/Update', 'post', data)
	},
  // 批量设置应用
  batchApp(data) {
    data.loading = true
		return axios.http('/App/Batch', 'post', data)
	},
  // 删除应用
  delApp(data) {
    data.loading = true
		return axios.http('/App/Delete', 'post', data)
	},


  /**
   * public api
   */
  // axios调用外部api
  getPubApi(url) {
    return axios.http(url, 'get', {});
  },
  // josnp跨域调用外部api
  jsonpAction(url) {
    url=url+'&output=jsonp&callback=getJsonpData'
    //动态创建script加载数据
    const JSONP_SCRIPT = document.getElementById('JSONP_SCRIPT')
    if (JSONP_SCRIPT) document.body.removeChild(JSONP_SCRIPT)
    const jsonp = document.createElement("script")
    jsonp.setAttribute('id', 'JSONP_SCRIPT')
    jsonp.src = url
    document.body.appendChild(jsonp)
    //动态创建getJsonpData函数执行回调脚本
    try {
      if(typeof(eval(getJsonpData))=="function"){
      //console.log('getJsonpData已存在')
      }
    } catch (error) {
      // console.log(error)
      const fun = document.createElement("script")
      fun.innerHTML = `
        function getJsonpData(r) {
          // console.log(r)
          localStorage.setItem('jsonpData',JSON.stringify(r))
        }
      `
      document.body.appendChild(fun)
    }
    // 加载script获取数据
    return new Promise((resolve, reject) => {
      jsonp.onload = function() {
        let d = localStorage.getItem('jsonpData')
        let r = null
        if (d && !util.isNull(d)) {
          r = JSON.parse(d)
          resolve(r)
        } else {
          reject(error)
        }
      }
    })
  }, 
}



